<?php
/**
 * Created by IntelliJ IDEA.
 * User: sunminqiang
 * Date: 17/3/8
 * Time: 上午10:28
 */
namespace app\admin\controller;
use app\admin\model\FriendshipLinkModel;
use think\Db;

class FriendshipLink extends Base
{
    /**
     * [index 友情链接列表]
     */
    public function index()
    {
        $key = input('key');
        $map = [];
        if ($key && $key !== "") {
            $map['name'] = ['like', "%" . $key . "%"];
        }
        $Nowpage = input('get.page') ? input('get.page') : 1;
        $limits = 10;// 获取总条数
        $count = Db::name('friendlylink')->where($map)->count();//计算总页面
        $allpage = intval(ceil($count / $limits));
        $contact= new FriendshipLinkModel();
        $lists = $contact->getFriendshipLinkByWhere($map, $Nowpage, $limits);
        $this->assign('Nowpage', $Nowpage); //当前页
        $this->assign('allpage', $allpage); //总页数
        $this->assign('count', $count);
        $this->assign('val', $key);
        if (input('get.page')) {
            return json($lists);
        }
        return $this->fetch();
    }
    /**
     * [index 友情链接列表]
     */
    public function add_link()
    {
        if(request()->isAjax()){

            $param = input('post.');
            $patent = new FriendshipLinkModel();
            $flag = $patent->insertFriendshipLink($param);
            return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
        }
        return $this->fetch();
    }

    public function edit_link()
    {
        $link= new FriendshipLinkModel();

        if(request()->isajax()){
            $param = input('post.');
            $res = array(
                'id'=>$param['id'],
                'name'=>$param['name'],
                'link'=>$param['link']
            );

            $flag = $link->updateFriendshipLink($res);
            return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
        }
        $id = input('param.id');
        $this->assign('link',$link->getOneFriendshipLink($id));
        return $this->fetch();
    }
    /**
     * [del_cate 删除分类]
     * @return [type] [description]
     */
    public function del_link()
    {
        $id = input('param.id');
        $cate = new FriendshipLinkModel();
        $flag = $cate->delFriendshipLink($id);
        return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
    }



}